local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local monthConfig = require("excel.MonthCard").card
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local ShopHelp = require("shop.ShopHelp")
local VipDefine = require("vip.VipDefine")
local Buffer = require("buffer.Buffer")
local SignLogic = require("fuli.SignLogic")
local HtmlUtil = require("common.HtmlUtil")
local Log = require("common.Log")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local PfLogic = require("tencentApi.PfLogic")

--月卡信息
function monthInfo(human)
  local sendMsg = Msg.gc.GC_MONTH_CARD_OPEN
  sendMsg.list[0] = #monthConfig
  local cache = {}
  for i=1,#monthConfig do
    sendMsg.list[i].reward[0] = #monthConfig[i].reward 
    for j=1, #monthConfig[i].reward do
      Grid.create(cache, monthConfig[i].reward[j][1], monthConfig[i].reward[j][2])
      Grid.makeItem(cache, sendMsg.list[i].reward[j], 0, human)
    end
    sendMsg.list[i].needYuanbao = monthConfig[i].needYuanbao
    if human.db.monthCard[i - 1].startTime == 0 then
      sendMsg.list[i].remainTime = 0
    else
      sendMsg.list[i].remainTime = VipDefine.MONTH_CARD_KEEP_TIME * human.db.monthCard[i - 1].times - (os.time() - human.db.monthCard[i - 1].startTime)
    end
    sendMsg.list[i].isGet = human.db.monthCard[i - 1].isGet
  end
  Msg.send(sendMsg, human.fd)
end
--打开月卡界面
function openWindow(human)
  monthInfo(human)
end
--激活其他奖励
function checkOtherReward(human)
  SignLogic.checkCanGet(human)
end
--激活会员
function jihuo(human, index, cnt)
  if index == 2 or (index == 1 and PfLogic.isAgentOther()) then
    if not ObjHuman.checkYuanbaoEnough(human, monthConfig[index].needYuanbao * cnt) then
      ShopHelp.onNoRMB(human)
      return false
    end
    ObjHuman.decYuanbao(human, monthConfig[index].needYuanbao * cnt, "monthCard")
  end
  if index == 2 then
    for i=1, #monthConfig[index].buffId do
      for j=1, cnt do
        Buffer.add(monthConfig[index].buffId[i], human, human)
      end
    end      
  end
  if human.db.monthCard[index - 1].startTime == 0 then
    local tb = {}
    local nowDay = os.date("*t", os.time())
    tb.year = nowDay.year
    tb.month = nowDay.month
    tb.day = nowDay.day
    tb.hour = 0
    tb.min = 0
    tb.sec = 0
    human.db.monthCard[index - 1].startTime = os.time(tb)
  end
  if human.db.monthCardAddBag == 0 and index == 2 then
    BagLogic.addVipCap(human, 48)
    human.db.monthCardAddBag = 1
    Broadcast.sendBroadcast(human, Lang.DOWN, Lang.MONTH_CARD_ADD_BAG)    
  end
  human.db.monthCard[index - 1].times = human.db.monthCard[index - 1].times + cnt  
  local tip = string.format(Lang.MONTH_CARD_BUY, HtmlUtil.fontName(human.name, human._id), index == 1 and "黄金月卡" or "至尊月卡")
  Broadcast.sendBroadcast2World(Lang.GROSSIP + Lang.UP, tip)  
  setStatus(human, index)
  monthInfo(human)
  Log.write(Log.LOGID_OSS_MONTH_CARD, human._id, human.account, human.name,human.db.lv, index)
end
--得到奖励
function getReward(human, index)
  if human.db.monthCard[index - 1].isGet ~= VipDefine.REWARD_CAN_GET then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.MONTH_CARD_CANOT_GET_REWARD)
  end
  if not BagLogic.checkCanAddItemList(human, monthConfig[index].reward, #monthConfig[index].reward) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.MONTH_CARD_BAG_NOT_ENOUGH)
  end
  human.db.monthCard[index - 1].isGet = VipDefine.REWARD_ALREADY_GET
  for j=1, #monthConfig[index].reward do
    BagLogic.addItem(human, monthConfig[index].reward[j][1], 1, monthConfig[index].reward[j][2], "monthCard")
  end  
  monthInfo(human)
  local name = (index == 1) and "黄金月卡" or "至尊月卡"
  local str = string.format(Lang.MONTH_CARD_GET_BRO, HtmlUtil.fontName(human.name, human._id), name)
  Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
end
--隔天重置状态
function setStatus(human, index)
  for i=1, #monthConfig do
    if human.db.monthCard[i - 1].startTime ~= 0 then
      if not index or i == index then
        human.db.monthCard[i - 1].isGet = VipDefine.REWARD_CAN_GET
      end
      
      if i == 2 then
        checkOtherReward(human)
      end
    end
  end
  monthInfo(human)
end
--查看至尊会员特权
function seeSpec(human)
  local sendMsg = Msg.gc.GC_MONTH_CARD_SEE_SPEC
  sendMsg.info = monthConfig[2].detail
  Msg.send(sendMsg, human.fd)
end
--检查月卡是否到期
function checkMonthCardExpire(human)
  for i=1,#monthConfig do
    if VipDefine.MONTH_CARD_KEEP_TIME * human.db.monthCard[i - 1].times - (os.time() - human.db.monthCard[i - 1].startTime) < 0 then
      human.db.monthCard[i - 1].startTime = 0
      human.db.monthCard[i - 1].times = 0
      human.db.monthCard[i - 1].isGet = VipDefine.REWARD_NOT_CAN_GET
    end
  end
end
--检查是否是至尊会员
function isHaveSpec(human)
  if human.db.monthCard[1].startTime ~= 0 then
    return true
  end
  return false
end
--返回多增加的经验，乘以的时候要加上1
function getMonthMoreExp(human)
  if isHaveSpec(human) then
    return monthConfig[2].addExpPercent / 10000
  end
  return 0
end
--返回额外的金钱掉落
function getMonthMoreYinliang(human)
  if isHaveSpec(human) then
    return monthConfig[2].addYinliangPer / 10000
  end
  return 0
end

--返回至尊会员buffId,一个table
function getMonthBuffId()
  return monthConfig[2].buffId
end

function checkAddMonthCardBag(human)
  if isHaveSpec(human) and human.db.monthCardAddBag == 0 then
    BagLogic.addVipCap(human, 48)
    human.db.monthCardAddBag = 1
    Broadcast.sendBroadcast(human, Lang.DOWN, Lang.MONTH_CARD_ADD_BAG)
  end
end

function buyMonthCard(human, oType, info)
  jihuo(human, 1, info.cnt)
end
InnerDataManagerFL.Register(InnerDataDefine.INNERDATA_TYPE_BUY_MONTHCARD, buyMonthCard)